import api from "../../api/index.js";

const state={
    classifyTitle:[],
    classifyRightType:[],
    classifyBottomType:[],
    classifyBottomSort:[],
    classifyFcid:""
};

const mutations={
    setClassifyTitle(state,nv){
        state.classifyTitle=nv;
    },
    setClassifyRightType(state,nv){
        state.classifyRightType=nv;
    },
    setClassifyBottomType(state,nv){
        state.classifyBottomType=nv;
    },
    setClassifyBottomSort(state,nv){
        state.classifyBottomSort=nv;
    },
    setClassifyFcid(state,nv){
        state.classifyFcid=nv;
    }
};

const actions={
    loadClassifyTitle(store){
        api.loadImgForHome({
            appPlat:"m",
            pids:117330 
        }).then((data)=>{
            // console.log(data);
            if(!data.success){
                return Promise.reject("网络错误!");
            };
            store.commit("setClassifyTitle",data.data[117330].list);
            return data.data[117330].list[0];
        }).then((list)=>{
            // console.log(list);
            store.dispatch("loadClassifyRightType",{
                pid:list.maitKey,
                fcid:list.miniWallkey
            });
            store.dispatch("loadClassifyBottomType",{
                fcid:list.miniWallkey,
                sort:"pop"
            });
        }).catch(api.showError);
    },
    loadClassifyRightType(store,obj){
        api.loadClassifyType({
            appPlat:"m",
            pid:obj.pid
        }).then((data)=>{
            if(!data.success){
                return Promise.reject("网络错误!");
            };
            store.commit("setClassifyRightType",data.data.list);
            store.commit("setClassifyFcid",obj.fcid);
            // console.log(data);
        }).catch(api.showError)
    },
    loadClassifyBottomType(store,obj){
        api.loadThinkYourLike({
            fcid:obj.fcid,
            page:1,
            sort:obj.sort,
            cKey:"h5-cube",
        }).then((data)=>{
            if(!data.success){
                return Promise.reject("网络错误!");
            };
            store.commit("setClassifyBottomSort",data.result.sortFilter);
            store.commit("setClassifyBottomType",data.result.wall.docs);
            // console.log(data);
        }).catch(api.showError)
    }
};

export default {
    state,
    mutations,
    actions
}